package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.ChargeScheduleAdapter;
import com.gears_software.sxdc.dccx.data.finder.IZPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.ChargeScheduleModel;
import com.gears_software.sxdc.dccx.data.model.FeeModel;
import com.gears_software.sxdc.dccx.data.model.OrderModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;


/**
 * Created by SunMuran on 2017/9/8.
 * 收费明细的activity
 */
@EActivity(R.layout.activity_charge_schedule)
public class ChargeScheduleActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView tv_youjiantou_icon;
    @ViewById
    RelativeLayout rl_guize;
    @ViewById
    TextView tv_all_money;
    @ViewById
    ListView lv_listView;
    @RestService
    IZPOrderFinder izpOrderFinder;
    @Bean
    GearsErrorHandler errorHandler;
    String id = "";
    ChargeScheduleModel chargeScheduleModel;
    OrderModel orderModel;
    List<FeeModel> list = new ArrayList<>();
    ChargeScheduleAdapter adapter;
    MaterialDialogProgress progress;

    @AfterViews
    void created() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        izpOrderFinder.setRestErrorHandler(errorHandler);
        chargeScheduleModel = new ChargeScheduleModel();
        orderModel = new OrderModel();
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        initIconfont();
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        getChargeSchedule();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        actionbar_title.setText("收费明细");
        right_icon.setVisibility(View.GONE);
        tv_youjiantou_icon.setTypeface(app.iconfont);
    }

    @Click
    void rl_guize() {
        Intent intent = new Intent();
        intent.putExtra("facetype", orderModel.getFacetype());
        intent.setClass(this, PlanningRulesActivity_.class);
        startActivity(intent);
    }

    @Background
    void getChargeSchedule() {
        ResultModel<ChargeScheduleModel> res = izpOrderFinder.getChargeSchedule(id);
        showResult(res);
    }

    @UiThread
    void showResult(ResultModel<ChargeScheduleModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                chargeScheduleModel = res.getData();
                orderModel = chargeScheduleModel.getOrder();
                tv_all_money.setText(orderModel.getFactprice().subtract(orderModel.getDiscountamount()).toString());
                if (chargeScheduleModel.getFee() != null) {
                    list = chargeScheduleModel.getFee();
                    adapter = new ChargeScheduleAdapter(this, list);
                    lv_listView.setAdapter(adapter);
                }
            }
        }
        progress.dismiss();
    }

    @Click
    void back_icon() {
        finish();
    }

}
